package com.eptok.gw.szcups.service;

import org.dom4j.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import ys8583.parse.IOMessage;

import com.eptok.gw.szcups.cups.ErrorCode;
import com.eptok.gw.szcups.dao.JdbcDao;
import com.eptok.gw.szcups.dao.bo.Cupsgwlog;
import com.eptok.gw.szcups.pojo.XmlMessage;
import com.solab.iso8583.common.ByteUtil;

public abstract class BaseService {
	
	protected static final Logger log = LoggerFactory.getLogger(BaseService.class);
	protected WebApplicationContext webApplicationContext= ContextLoader.getCurrentWebApplicationContext();
	
	protected Document document;
	
	public abstract IOMessage process(XmlMessage xmlMessage)throws Exception;
	
	
	protected long insertlog(XmlMessage xmlMessage,IOMessage iomessage){
		JdbcDao jdbcDao = (JdbcDao)webApplicationContext.getBean("jdbcDao");
		long id = jdbcDao.querySequences("CUPSGWLOG_SEQ");
		Cupsgwlog cupsgwlog = new Cupsgwlog();
		cupsgwlog.setId(id);
		cupsgwlog.setAcceptCode((String)iomessage.getRspField(42));
		cupsgwlog.setAcceptName((String)iomessage.getRspField(43));
		cupsgwlog.setAcceptTermianl((String)iomessage.getRspField(41));
		cupsgwlog.setAcquireInstitution((String)iomessage.getRspField(32));
		if((String)iomessage.getRspField(4) != null){
			cupsgwlog.setAmount(Double.parseDouble((String)iomessage.getRspField(4)));
		}
		String bankaccount = "";
		if(iomessage.getRspField(35)!=null){
			bankaccount = ((String)iomessage.getRspField(35)).split("=")[0];
		}
		cupsgwlog.setBankAccount(bankaccount);
		cupsgwlog.setConditionCode((String)iomessage.getRspField(25));
		cupsgwlog.setCurrencyCode((String)iomessage.getRspField(49));
		cupsgwlog.setCustomizingMessage((String)iomessage.getRspField(48));
		cupsgwlog.setEntryMode((String)iomessage.getRspField(22));
		cupsgwlog.setForwardInstitution((String)iomessage.getRspField(33));
		cupsgwlog.setGateWayType("01");
		cupsgwlog.setHexData(ByteUtil.bytesToHexString(iomessage.toGetByte()));
		cupsgwlog.setMerchantId(xmlMessage.getMerchantId());
		cupsgwlog.setMessageCode(xmlMessage.getMsgCode());
		cupsgwlog.setMessageId((String)iomessage.getRspMessageID());
		cupsgwlog.setProcessCode((String)iomessage.getRspField(3));
		cupsgwlog.setReqtraceId(xmlMessage.getReqSerialNumber());
		cupsgwlog.setRequetor(xmlMessage.getSender());
		cupsgwlog.setRetrievalNumber((String)iomessage.getRspField(37));
		cupsgwlog.setSytemtrace((String)iomessage.getRspField(11));
		cupsgwlog.setStatus("1");
		jdbcDao.insertCupsLog(cupsgwlog);
		return id;
	}
	
	protected void updatelog(long id,XmlMessage xmlMessage,IOMessage iomessage) throws Exception{
		JdbcDao jdbcDao = (JdbcDao)webApplicationContext.getBean("jdbcDao");
		Cupsgwlog cupsgwlog = new Cupsgwlog();
		cupsgwlog.setId(id);
		cupsgwlog.setResponseCode(xmlMessage.getStatus());
		cupsgwlog.setRsptraceId(xmlMessage.getRspSerialNumber());
		if(xmlMessage.getStatus().equals("00")){
			cupsgwlog.setStatus("2");
		}else{
			cupsgwlog.setStatus("3");
		}
		if(iomessage != null){
			cupsgwlog.setSettlement(iomessage.getReqMesField(15)==null?"":(String)iomessage.getReqMesField(15));
		}
		cupsgwlog.setDescription(ErrorCode.map.get(xmlMessage.getStatus()));
		jdbcDao.updateCupsLog(cupsgwlog);
	}
	

}
